<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * CLASS - CONTACT US
 * 
 * @name      contact_us
 * @package		OS CRM
 * @category  Class
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------
class contact_us extends Controller {

  /*common class varibale*/
  var $c 	= 'contact_us';//current controler
	var $p_c = '';//parent controller
  
  //set db - listing data
	var $e_list_limit 	= '10';
	var $e_list_offset 	= '0';
	var $e_list_order   = 'asc';
	var $e_list_order_by= ''; 
  
  /*message strings*/
  var $valid_success 	= '';
  var $valid_errors 	= '';
  var $valid_notice 	= '';
  
  /**
   * contact_us  - constructor of the class
   * 
   * @access public
   * @param  NA
   * @return void
   */
	function contact_us()
	{
		parent::Controller();	
	}
	
  /** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{
    $this->reg();
	}
  
  
  /** 
	 * reg - registration
	 *
	 * @desc NA
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
  function reg()
	{
    $id = 0;
		$token = random_string();
    $post_arr = array();
    $post_arr['os_mem_ip_add'] = $_SERVER['SERVER_ADDR']; 
    if ($_POST) {
      $post_arr = $this->_set_post_data();   
    }
    
		$data = array();
		$data['cont_title'] = 'Contact Us';
    
    $data['post_arr']     = $post_arr;
		
		/*set crontroller info*/
		$data['c'] 	= $this->c;
		$data['p_c'] 	= $this->p_c;
		    
    /*set form actions*/
    $data['frm_name']   = 'frmmemberadd';
    $data['frm_action'] = site_url( $this->c .'/insert');
    $data['frm_token']  = $token;
    $data['frm_mode']   = 'add';
    $data['frm_id']      = $id;
            
    $this->session->set_userdata('frm_token', $token);
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->valid_errors;
    $data['msg_info']['valid_notice'] = $this->valid_notice;
    $data['msg_info']['valid_success'] = $this->valid_success;
    
    __display('registration',$data);
	} 
  
  /** 
	 * Insert
	 *
	 * @desc this will validate and insert a new record
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function insert()
	{
		$this->load->library('form_validation');
		$login_status     = false;
		$data             = array();
    $frm_token        = $this->input->post('frm_token');
		$sess_frm_token   = $this->session->userdata('frm_token');
    $this->session->unset_userdata('frm_token');
		$errors = $this->_val_form();     
		if(!empty($errors)){
			$this->valid_errors =  $errors;
      $this->reg();
    }else if($frm_token != $sess_frm_token){      
			$this->valid_errors =  '<p>&raquo; Sorry form cannot be posted twice, please re-enter the details</p>';
      $this->reg();
    }else{
      $data = array();
      $data['os_contact_name'] = $this->input->post('os_contact_name');
      $data['os_contact_add1'] = $this->input->post('os_contact_add1');
      $data['os_contact_city'] = $this->input->post('os_contact_city');
      $data['os_contact_state'] = $this->input->post('os_contact_state');
      $data['os_contact_postcode'] = $this->input->post('os_contact_postcode');
      $data['os_contact_country'] = $this->input->post('os_contact_country');
      $data['os_contact_email'] = $this->input->post('os_contact_email');      
      $data['os_contact_phone'] = $this->input->post('os_contact_phone');
      $data['os_contact_fax'] = $this->input->post('os_contact_fax');
      $data['os_contact_mobile'] = $this->input->post('os_contact_mobile');
      $data['os_contact_comments']    = filter_data($this->input->post('os_contact_comments'));
      $data['os_contact_status'] = '1';
      
      $data['os_contact_created'] = date(DB_DATE_FORMAT);
      $data['os_contact_ref_ip'] = $this->input->ip_address();
      $data['os_contact_ref_agent'] = $this->input->user_agent();      
           
      $ref_url = $this->input->server('HTTP_REFERER');
      if(empty($ref_url )){
        $ref_url = $this->input->server('REQUEST_URI');
      }
      $data['os_contact_ref_url'] = $ref_url;
      
      $this->db->insert(TBL_CONTACT_US, $data); 
      $last_id = $this->db->insert_id();
      //__debug($last_id);
      
      $this->load->plugin('phpmailer');
      /*send email to admin*/
      $subject = OS_CRM_FROM_NAME . ' - Enquiry '. __set_date(date(DB_DATE_FORMAT), 4);
      $post_arr = $this->_set_post_data();   
      $message = $this->load->view('email_admin',array('post_arr'=>$data),TRUE);
      send_smtp_email_admin($subject, $message);
      
      /*send email to user*/
      $subject = OS_CRM_FROM_NAME . ' - Admin Enquiry '. __set_date(date(DB_DATE_FORMAT), 4);
      $post_arr = $this->_set_post_data();   
      $message = $this->load->view('email_user',$data,TRUE);
      send_smtp_email($post_arr['os_contact_email'], $post_arr['os_contact_name'], $subject, $message);
      
      
      redirect($this->c.'/thankyou');
    }
	}
  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _val_form()
	{
		$config =  array();
    $config[] =  array('field' => 'os_contact_name',  'label' => 'Name',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_contact_email', 'label' => 'Email', 'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_contact_phone',     'label' => 'Phone',     'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_contact_fax',       'label' => 'Fax',       'rules' => 'trim|min_length[2]|xss_clean');
    
    $config[] =  array('field' => 'os_contact_add1',  'label' => 'Add1',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_contact_city',  'label' => 'City',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_contact_state', 'label' => 'State', 'rules' => 'trim|min_length[2]|xss_clean');    
    $config[] =  array('field' => 'os_contact_postcode',  'label' => 'Postcode',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_contact_country',   'label' => 'Country',   'rules' => 'trim|min_length[2]|xss_clean');
    
    
        
		$this->form_validation->set_rules($config); 
		$this->form_validation->set_error_delimiters('<p>&raquo; ', '</p>');
		if ($this->form_validation->run() == false)
		{
			return validation_errors(); 
		}
	}	
  
  /** 
	 * thankyou - thank you message
	 *
	 * @desc NA
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
  function thankyou()
	{
    $data = array();
		$data['cont_title'] = 'Contact Us - Thankyou';
    
		/*set crontroller info*/
		$data['c'] 	= $this->c;
		$data['p_c'] 	= $this->p_c;
		    
    __display('thankyou',$data);
	}
  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _set_post_data()
	{
    $var = array();    
    if(isset($_POST)){      
      foreach($_POST as $key => $val){
        $var[$key] = $this->input->post($key); 
      }
    }    
    return $var;
  }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */